package com.wly.login.user.strategy;

import cn.hutool.core.util.StrUtil;
import com.wly.login.common.ValidationException;
import com.wly.login.user.entity.User;
import com.wly.login.user.service.UserService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

/**
 * @author wly
 */
@Service
public class EmailLoginStrategy implements LoginStrategy{

	@Override
	public String loginType() {
		return "email";
	}

	@Resource
	private UserService userService;

	@Override
	public void login(User user) {
		String email = user.getEmail();
		String password = user.getPassword();
		// 1、参数校验
		if (StrUtil.isBlank(email)) {
			throw new ValidationException("邮箱不能为空");
		}
		if (StrUtil.isBlank(password)) {
			throw new ValidationException("密码不能为空");
		}
		// 2、验证用户是否存在
		User userInfo = userService.findByEmail(email);
		if (userInfo == null) {
			throw new ValidationException("邮箱不存在");
		}
		// 3、校验密码
		if(!password.equals(userInfo.getPassword())) {
			throw new ValidationException("密码错误,请重试");
		}
		user.setId(userInfo.getId());
		// 实现账号密码登录的逻辑
		System.out.println("邮箱登录 - 邮箱: " + email + ", 密码: " + password);
	}
}
